package com.fessc.develop.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fessc.develop.entity.ccbvo.CCBResponseHeadVo;
import com.fessc.develop.utils.BusiUtil;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * 建行回调返回结果
 */
@Getter
@Setter
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class CCBResult<T> implements Serializable {

    @JSONField(serialize = false)
    private String statusCode;

    @JSONField(serialize = false)
    private String statusMsg;

    @JsonProperty("head")
    private CCBResponseHeadVo head;

    @JsonProperty("body")
    private T body;

    public CCBResult() {

    }

    public CCBResult(String statusCode, String statusMsg) {
        this.statusCode = statusCode;
        this.statusMsg = statusMsg;
    }

    public CCBResult(CCBResponseHeadVo head, T body) {
        this.head = head;
        this.body = body;
    }

    public void setResponseHeaders(String org, String coPlfID, String msgBsnTp, String reqTraceId, String resTraceId) {
        if (BusiUtil.isNotNull(reqTraceId)) {
            this.head.setReqtraceid(reqTraceId);
        }
        if (BusiUtil.isNotNull(resTraceId)) {
            this.head.setRestraceid(resTraceId);
        }
        if (BusiUtil.isNotNull(org)) {
            this.head.setOrg(org);
        }
        if (BusiUtil.isNotNull(coPlfID)) {
            this.head.setCoplfid(coPlfID);
        }
        if (BusiUtil.isNotNull(msgBsnTp)) {
            this.head.setMsgbsntp(msgBsnTp);
        }
    }

    /**
     * 重写toString方法，让Result对象以json字符串形式存在
     *
     * @return Json字符串
     */
    @Override
    public String toString() {
        JSONObject json = new JSONObject();
        if (null != this.head) {
            json.put("head", JSON.toJSONString(this.head));
        }
        if (null != this.body) {
            json.put("body", JSON.toJSONString(this.body));
        }
        return json.toJSONString();
    }
}